package vn.gq.udv.db;

import java.util.List;

import vn.gq.tym.storytemplate.GQConst;
import vn.gq.udv.db.items.Category;
import vn.gq.udv.db.items.ChapterDetail;
import vn.gq.udv.db.items.ChapterItem;
import vn.gq.udv.utils.FileHelper;
import vn.gq.udv.utils.GQUtils;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public abstract class DatabaseHelper extends SQLiteOpenHelper {
	
	public static final String TAG = DatabaseHelper.class.getSimpleName();
	public static final String DBNAME;
	public static final String DBPATH;
	public static final String DBPATHSD;
	public static final int VERSION;
	protected Context mContext;
	protected SQLiteDatabase mSQLiteDb;
	
	static {
		DBNAME = GQConst.DB.DATABASENAME;
		DBPATH = GQConst.DB.DATABASEPATH;
		DBPATHSD = GQConst.DB.DATABASEPATH_SD;
		VERSION = 1;
	}
	
	public DatabaseHelper(Context context) {
		super(context, DBNAME, null, VERSION);
		mContext = context;
		copyDatabase();
	}
	
	public DatabaseHelper(Context context, String dbName, CursorFactory factory,
			int version) {
		super(context, dbName, factory, version);
		copyDatabase();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		copyDatabase();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		onCreate(db);
	}
	
	public boolean sdcardAvailable() {
		return GQUtils.sdCardIsReady() && GQConst.DB.PreferExternalStorage; 
	}
	
	public void createDatabase() {
		if (!isExists()) {
			getReadableDatabase();
			copyDatabase();
		}
	}
	
	public synchronized void openDatabase() {
		if (mSQLiteDb == null) {
			try {
				mSQLiteDb = SQLiteDatabase.openDatabase(getDatabasePath(), null, SQLiteDatabase.OPEN_READWRITE);
			} catch(SQLiteException e) {
				e.printStackTrace();
			}
		}
	}
	
	public SQLiteDatabase getDatabase() {
		return SQLiteDatabase.openDatabase(getDatabasePath(), null, SQLiteDatabase.OPEN_READONLY);
	}
	
	private void copyDatabase() {
		if (!isExists()) {
			String path = getDatabasePath();
			FileHelper fileHelper = FileHelper.getInstance(mContext);
			if (fileHelper.isExistsFileAsset(DBNAME)) {
				fileHelper.copy(DBNAME, path, GQConst.FILE_TYPE);
			} else {
				fileHelper.mergeAndCopy(DBNAME, path, GQConst.FILE_TYPE);
			}
		}
	}
	
	public String getDatabasePath() {
		if (sdcardAvailable()) return DBPATHSD;
		return DBPATH;
	}
	
	public boolean isExists() {
		boolean ret = false;
		SQLiteDatabase db = null;
		try {
			db = SQLiteDatabase.openDatabase(getDatabasePath(), null, SQLiteDatabase.OPEN_READONLY);
			if (db != null)
				ret = true;
		} catch(SQLiteException e) {
		} finally {
			try {
				if (db != null) {
					if (db.isOpen()) db.close();
				}
			} catch(Exception e) {
			}
		}
		return ret;
	}

	@Override
	public synchronized void close() {
		try {
			if (mSQLiteDb != null) {
				if (mSQLiteDb.isOpen()) mSQLiteDb.close();
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		super.close();
	}

	public abstract List<ChapterItem> getChapterItems(int catId);
	
	public abstract List<ChapterItem> getChapterItems();
	
	public abstract ChapterDetail getChapterDetailById(int id);
	
	public abstract List<Category> getCategories();
	
	public abstract Category getCategoryById(int catId);
}
